package com.codes.controller;

import com.codes.core.common.result.Result;
import com.codes.model.Project;
import com.codes.service.ProjectSV;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.collections4.ResettableListIterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2020/6/2 9:33
 * @Created by peanut
 */
@Api(tags = "项目接口")
@RequestMapping("/project")
@RestController
public class ProjectCtr {

    @Autowired
    private ProjectSV projectSV;

    @ApiOperation("分页")
    @GetMapping("/page")
    public Result getPage(@RequestParam(defaultValue = "1", required = true) int page,
                          @RequestParam(defaultValue = "10", required = true) Integer limit,
                          @RequestParam Map<String, Object> map) {

        PageHelper.startPage(page, limit);
        List<Map<String, Object>> users =projectSV.getListByMap(map);
        PageInfo<Map<String, Object>> info = new PageInfo<>(users);
        return Result.pages(info.getList(), (int) info.getTotal());

    }

    @ApiOperation("获取所有项目")
    @GetMapping("/getall")
    public Result getAllProject(@RequestParam Map<String, Object> map){

        List<Map<String, Object>> users =projectSV.getListByMap(map);
        return Result.success(users);

    }

    @ApiOperation("获取项目详情")
    @PostMapping("/getproject-info")
    public Result getPage(  @RequestParam Map<String, Object> map) {

        List<Map<String, Object>> list =projectSV.getListByMap(map);
        if(list.size()> 0 ){
            return Result.success(list.get(0));
        }
        return Result.failed("获取失败");
    }

    @ApiOperation("添加项目")
    @PostMapping("/save")
    public Result addProject( @RequestParam Map<String, Object> map){

        int re =0;
        String name = MapUtils.getString(map,"name");
        if(null == name) return Result.failed("项目名称请填写");
        Integer projectId = MapUtils.getInteger(map,"projectId");
        HashMap<String,Object> searchMap = new HashMap<>();
        searchMap.put("name",name);
        List<Map<String, Object>> list =projectSV.getListByMap(searchMap);
        if(null == projectId){
                if(list.size()>0){
                    return Result.failed("此项目名称已存在");
                }
            re = projectSV.insertByMap(map);

        }else {
            Integer addId =null;
            if(list.size()>0){
                addId = MapUtils.getInteger(list.get(0),"projectId");
                if(!projectId.equals(addId))  return Result.failed("此项目名称已存在");
            }
            re= projectSV.updateByMap(map);
        }
        if(re > 0 ){
            return  Result.success("项目添加修改成功",re);
        }
        return Result.failed("操作失败");
    }

    @ApiOperation("删除项目")
    @PostMapping("/del")
    public Result delete( @RequestParam Map<String, Object> map){
        Integer projectId = MapUtils.getInteger(map,"projectId");
        int re = 0 ;
        if(null == projectId){
            return Result.failed("项目id为空");
        }else {
            re = projectSV.deleteByPrimaryKey(projectId);
        }

        if(re>0){
            return Result.success();
        }
        return Result.failed("项目删除失败");
    }




}